package cn.fxl.service.impl;

import cn.fxl.ajax.AjaxResult;
import cn.fxl.domain.MoveUser;
import cn.fxl.domain.Movie;
import cn.fxl.domain.User;
import cn.fxl.feign.MovieFeign;
import cn.fxl.mapper.UserMapper;
import cn.fxl.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MovieFeign movieFeign;
    @Override
    public AjaxResult buyticket(User user) {
      User user1= userMapper.getOne(user.getId());
        if(user1.getBalance()>=user.getBalance()){
            user1.setBalance(user1.getBalance()-user.getBalance());
            userMapper.update(user1);
            return new AjaxResult();
        }else {
            return new AjaxResult(false,"余额不足");
        }
    }

    @Override
    public AjaxResult userAdd(User user) {
        userMapper.userAdd(user);
        List<Movie> movies = movieFeign.movieGetAll();
        Movie movie = movies.get(0);
        MoveUser moveUser = new MoveUser();
        moveUser.setUser_id(user.getId());
        moveUser.setMovie_ticket_id(movie.getId());
        moveUser.setStatus(0);
        movieFeign.saveAdd(moveUser);
        movie.setStock(movie.getStock()-1);
        movieFeign.UpdateByMovie(movie);
        return new AjaxResult();
    }
}
